package fr.enseirb.mycub.component.map.bus;

import java.util.ArrayList;
import java.util.Hashtable;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.util.Log;
import android.widget.FrameLayout;

import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import fr.enseirb.mycub.R;
import fr.enseirb.mycub.component.map.OverlayBundle;
import fr.enseirb.mycub.component.map.OverlayBundleItem;
import fr.enseirb.mycub.structures.Line;
import fr.enseirb.mycub.structures.SpecificLine;
import fr.enseirb.mycub.structures.bus.BusStation;

public class BusStationsOverlay extends OverlayBundle {

	private ArrayList<? extends OverlayBundleItem> items = new ArrayList<BusStationOverlay>();
	static ClassicDrawable standard = null;
	static PinDrawable pin = null;
	static Paint mTextPaint = new Paint();
	static Paint mPinPaint = new Paint();
	static boolean mDrawText;
	private static Hashtable<String, BitmapDrawable> table;

	int IMAGE_HEIGHT;

	public BusStationsOverlay(Resources resources, BitmapDrawable BusPin, MapView mv, Context context) {
		//this(resources,drawable1,mv,context);
		super(BusPin, mv, context);

		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(19);
		mTextPaint.setTextAlign(Align.RIGHT);
		mTextPaint.setColor(Color.BLACK);
		mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

		mPinPaint.setStyle(Paint.Style.FILL);
		//mPinPaint.setColor(Color.BLUE);
		mPinPaint.setAlpha(150);
		mPinPaint.setAntiAlias(true);
		
		Hashtable<String, BitmapDrawable> table = new Hashtable<String, BitmapDrawable>();
		

		BusStationsOverlay.standard = new ClassicDrawable(resources, BusPin.getBitmap());
		BusStationsOverlay.table = table;
		setupTable(table, context);
		
		boundCenterBottom(standard);
		pin = new PinDrawable();
		ClassicDrawable.mPaint.setColor(Color.WHITE);
		IMAGE_HEIGHT = standard.getIntrinsicHeight();
		
		populate();
	}



	@Override
	public void setItems(ArrayList<? extends OverlayBundleItem> list) {
		items = list;
		setLastFocusedIndex(-1);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return (items.get(i));
	}

	public ArrayList<? extends OverlayBundleItem> getOverlayList() {
		return items;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow) {
			if (mapView.getZoomLevel() >= 16) {
				mDrawText = true;
				setBalloonBottomOffset(IMAGE_HEIGHT);
			} else {
				mDrawText = false;
				setBalloonBottomOffset(0);
			}
			super.draw(canvas, mapView, shadow);
		}
	}

	@Override
	public int size() {
		return (items.size());
	}

	public ArrayList<? extends OverlayBundleItem> getOverlaysFromCursor(
			ArrayList<Cursor> stationsCursors, Location location, int distanceFilter) {
		Log.i("BusstationsOverlay", " getting overlays...");
		Cursor stationsCursor = stationsCursors.get(0);
		Cursor lineCursor = stationsCursors.get(1);
		Cursor specificLinesCursor = stationsCursors.get(2);
		Cursor specificLineStationCursor = stationsCursors.get(3);
		
		ArrayList<Line> lines = Line.getLinesFromCursor(lineCursor);
		ArrayList<SpecificLine> specificLines = SpecificLine.getSpecificLinesFromCursor(lines, specificLinesCursor);
		Log.i("BusstationsOverlay", " going for stations !!!");
		ArrayList<BusStation> stations = BusStation.getSimpleStationFromCursor(specificLines, stationsCursor, specificLineStationCursor);
		Log.i("BusstationsOverlay", " got them ! Lets do the overlay items...");
		ArrayList<OverlayBundleItem> overlays = new ArrayList<OverlayBundleItem>(
				stationsCursor.getCount());
		BusStationOverlay overlay;
		Location stationLocation = new Location("");
		float distance = 0;
		for(int i = 0; i < stations.size(); i++) {
		 	final BusStation station = stations.get(i);
			overlay = new BusStationOverlay(
					station.getGeoPoint(),
					station.getLine(),
					station.getId());
			

			if (distanceFilter != 0 && location != null) {
				stationLocation.setLatitude(((double) station.getGeoPoint().getLatitudeE6()) * 1E-6);
				stationLocation.setLongitude(((double) station.getGeoPoint().getLongitudeE6()) * 1E-6);
				distance = location.distanceTo(stationLocation);
				if (distance > distanceFilter) {
					continue;
				}
			}
			overlays.add(overlay);
		}
//		} while (stationsCursor.moveToNext());
		Log.i("BusstationsOverlay", "--> overlays : "+overlays.size());
		return overlays;
	}

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		Log.i("BusStations", "ON BALLOON TAP");
		return true;
	}



	@Override
	protected FrameLayout createBalloonOverlayView() {
		BalloonOverlayViewStationsBus<OverlayItem> b =  new BalloonOverlayViewStationsBus<OverlayItem>(getMapView().getContext(),
				viewOffset);
		return b;
	}

	public static Hashtable<String, BitmapDrawable> getTable(Context context){
		if(table == null) setupTable(new Hashtable<String, BitmapDrawable>(), context);
		return table;
	}
	
	public static void setupTable(Hashtable<String, BitmapDrawable> table, Context context){
		BitmapDrawable ligne1 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i01);
		table.put("1", ligne1);
		BitmapDrawable ligne2 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i02);
		table.put("2", ligne2);
		BitmapDrawable ligne3 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i03);
		table.put("3", ligne3);
		BitmapDrawable ligne4 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i04);
		table.put("4", ligne4);
		BitmapDrawable ligne5 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i05);
		table.put("5", ligne5);
		BitmapDrawable ligne6 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i06);
		table.put("6", ligne6);
		BitmapDrawable ligne7 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i07);
		table.put("7", ligne7);
		BitmapDrawable ligne8 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i08);
		table.put("8", ligne8);
		BitmapDrawable ligne9 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i09);
		table.put("9", ligne9);
		BitmapDrawable ligne10 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i10);
		table.put("10", ligne10);
		BitmapDrawable ligne11 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i11);
		table.put("11", ligne11);
		BitmapDrawable ligne14 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i14);
		table.put("14", ligne14);
		BitmapDrawable ligne15 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i15);
		table.put("15", ligne15);
		BitmapDrawable ligne16 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i16);
		table.put("16", ligne16);
		BitmapDrawable ligne20 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i20);
		table.put("20", ligne20);
		BitmapDrawable ligne21 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i21);
		table.put("21", ligne21);
		BitmapDrawable ligne23 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i23);
		table.put("23", ligne23);
		BitmapDrawable ligne24 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i24);
		table.put("24", ligne24);
		BitmapDrawable ligne26 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i26);
		table.put("26", ligne26);
		BitmapDrawable ligne27 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i27);
		table.put("27", ligne27);
		BitmapDrawable ligne28 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i28);
		table.put("28", ligne28);
		BitmapDrawable ligne29 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i29);
		table.put("29", ligne29);
		BitmapDrawable ligne30 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i30);
		table.put("30", ligne30);
		BitmapDrawable ligne32 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i32);
		table.put("32", ligne32);
		BitmapDrawable ligne33 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i33);
		table.put("33", ligne33);
		BitmapDrawable ligne34 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i34);
		table.put("34", ligne34);
		BitmapDrawable ligne35 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i35);
		table.put("35", ligne35);
		BitmapDrawable ligne36 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i36);
		table.put("36", ligne36);
		BitmapDrawable ligne37 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i37);
		table.put("37", ligne37);
		BitmapDrawable ligne38 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i38);
		table.put("38", ligne38);
		BitmapDrawable ligne39 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i39);
		table.put("39", ligne39);
		BitmapDrawable ligne40 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i40);
		table.put("40", ligne40);
		BitmapDrawable ligne41 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i41);
		table.put("41", ligne41);
		BitmapDrawable ligne42 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i42);
		table.put("42", ligne42);
		BitmapDrawable ligne43 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i43);
		table.put("43", ligne43);
		BitmapDrawable ligne44 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i44);
		table.put("44", ligne44);
		BitmapDrawable ligne45 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i45);
		table.put("45", ligne45);
		BitmapDrawable ligne46 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i46);
		table.put("46", ligne46);
		BitmapDrawable ligne47 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i47);
		table.put("47", ligne47);
		BitmapDrawable ligne48 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i48);
		table.put("48", ligne48);
		BitmapDrawable ligne49 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i49);
		table.put("49", ligne49);
		BitmapDrawable ligne50 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i50);
		table.put("50", ligne50);
		BitmapDrawable ligne51 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i51);
		table.put("51", ligne51);
		BitmapDrawable ligne52 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i52);
		table.put("52", ligne52);
		BitmapDrawable ligne54 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i54);
		table.put("54", ligne54);
		BitmapDrawable ligne55 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i55);
		table.put("55", ligne55);
		BitmapDrawable ligne56 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i56);
		table.put("56", ligne56);
		BitmapDrawable ligne57 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i57);
		table.put("57", ligne57);
		BitmapDrawable ligne58 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i58);
		table.put("58", ligne58);
		BitmapDrawable ligne62 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i62);
		table.put("62", ligne62);
		BitmapDrawable ligne63 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i63);
		table.put("63", ligne63);
		BitmapDrawable ligne64 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i64);
		table.put("64", ligne64);
		BitmapDrawable ligne67 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i67);
		table.put("67", ligne67);
		BitmapDrawable ligne68 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i68);
		table.put("68", ligne68);
		BitmapDrawable ligne70 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i70);
		table.put("70", ligne70);
		BitmapDrawable ligne71 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i71);
		table.put("71", ligne71);
		BitmapDrawable ligne72 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i72);
		table.put("72", ligne72);
		BitmapDrawable ligne73 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i73);
		table.put("73", ligne73);
		BitmapDrawable ligne74 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i74);
		table.put("74", ligne74);
		BitmapDrawable ligne76 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i76);
		table.put("76", ligne76);
		BitmapDrawable ligne77 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i77);
		table.put("77", ligne77);
		BitmapDrawable ligne78 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i78);
		table.put("78", ligne78);
		BitmapDrawable ligne79 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i79);
		table.put("79", ligne79);
		BitmapDrawable ligne80 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i80);
		table.put("80", ligne80);
		BitmapDrawable ligne83 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i83);
		table.put("83", ligne83);
		BitmapDrawable ligne86 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i86);
		table.put("86", ligne86);
		BitmapDrawable ligne87 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i87);
		table.put("87", ligne87);
		BitmapDrawable ligne88 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i88);
		table.put("88", ligne88);
		BitmapDrawable ligne89 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i89);
		table.put("89", ligne89);
		BitmapDrawable ligne90 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i90);
		table.put("90", ligne90);
		BitmapDrawable ligne91 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i91);
		table.put("91", ligne91);
		BitmapDrawable ligne92 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i92);
		table.put("92", ligne92);
		BitmapDrawable ligne93 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i93);
		table.put("93", ligne93);
		BitmapDrawable ligne94 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i94);
		table.put("94", ligne94);
		BitmapDrawable ligne95 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i95);
		table.put("95", ligne95);
		BitmapDrawable ligne96 = (BitmapDrawable) context.getResources().getDrawable(R.drawable.i96);
		table.put("96", ligne96);
	}


}